package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("CrawlerfileController")
public class CrawlerfileController extends CmiEmaController {

	@RequestMapping("/listCrawlerfiles")
	public ModelAndView listCrawlerfiles(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = crawlerfilePaginationService.updatePagination(request, pagination);
		Set<Crawlerfile> crawlerfiles = crawlerfilePaginationService.getPaginationCrawlerfiles(pagination);

		mav.addObject("crawlerfiles", crawlerfiles);
		mav.addObject("entityName", "crawlerfile");
		mav.addObject("pagination", pagination);
		mav.setViewName("crawlerfile/listCrawlerfiles");
		return mav;
	}

	public String listCrawlerfiles() {
		return "redirect:/listCrawlerfiles";
	}

	@RequestMapping("/viewCrawlerfile")
	public ModelAndView viewCrawlerfile(@RequestParam Integer crawlerfileId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("crawlerfile", crawlerfileDAO.findCrawlerfileByPrimaryKey(crawlerfileId));
		mav.setViewName("crawlerfile/viewCrawlerfile");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteCrawlerfiles")
	public ModelAndView confirmDeleteCrawlerfiles(
			@RequestParam(value = "deleteCrawlerfilesIds", required = true) Set<Integer> deleteCrawlerfilesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Crawlerfile> deleteCrawlerfiles = crawlerfileDAO.findCrawlerfilesByIds(deleteCrawlerfilesIds);
		Set<Crawlerfile> referencedCrawlerfiles = crawlerfileService.loadReferenceCrawlerfiles(deleteCrawlerfiles);
		boolean hasReferenced;
		if (referencedCrawlerfiles.size() == 0) {
			mav.addObject("deleteEntitys", deleteCrawlerfiles);
			hasReferenced = false;
		} else {
			deleteCrawlerfiles.removeAll(referencedCrawlerfiles);
			mav.addObject("referencedEntitys", referencedCrawlerfiles);
			mav.addObject("deleteEntitys", deleteCrawlerfiles);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Crawlerfile");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listCrawlerfiles");
		return mav;
	}

	@RequestMapping("/deleteCrawlerfile")
	public String deleteCrawlerfile(@RequestParam Integer crawlerfileId) {
		Crawlerfile crawlerfile = crawlerfileDAO.findCrawlerfileByPrimaryKey(crawlerfileId);
		crawlerfileService.deleteCrawlerfile(crawlerfile);
		return "forward:/listCrawlerfiles";
	}

	@RequestMapping("/confirmDeleteCrawlerfile")
	public ModelAndView confirmDeleteCrawlerfile(@RequestParam Integer crawlerfileId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("crawlerfile", crawlerfileDAO.findCrawlerfileByPrimaryKey(crawlerfileId));

		mav.setViewName("crawlerfile/deleteCrawlerfile");

		return mav;
	}

	@RequestMapping("/deleteCrawlerfiles")
	public String deleteCrawlerfiles(
			@RequestParam(value = "deleteCrawlerfilesId", required = true) Set<Integer> deleteCrawlerfilesId) {
		crawlerfileService.deleteCrawlerfiles(deleteCrawlerfilesId);
		return "forward:/listCrawlerfiles";
	}

	@RequestMapping("/editCrawlerfile")
	public ModelAndView editCrawlerfile(@RequestParam Integer crawlerfileId) {

		ModelAndView mav = new ModelAndView();

		Crawlerfile crawlerfile = crawlerfileDAO.findCrawlerfileByPrimaryKey(crawlerfileId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("crawlerfile", crawlerfile);

		// 把所有的引用crawlerfile对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerfileService.loadCrawlerfileObjectReferencedSet(crawlerfile));

		// 将编辑页面crawlerfile添加到mav中
		mav.setViewName("crawlerfile/editCrawlerfile");
		return mav;
	}

	@RequestMapping("/newCrawlerfile")
	public ModelAndView newCrawlerfile() {
		ModelAndView mav = new ModelAndView();
		Crawlerfile crawlerfile = new Crawlerfile();

		mav.addObject("crawlerfile", crawlerfile);

		// 把所有的引用crawlerfile对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerfileService.loadCrawlerfileObjectReferencedSet(crawlerfile));

		mav.addObject("newFlag", true);
		mav.setViewName("crawlerfile/editCrawlerfile");

		return mav;
	}

	@RequestMapping("/saveCrawlerfile")
	public ModelAndView saveCrawlerfile(@ModelAttribute Crawlerfile crawlerfile) {

		ModelAndView mav = new ModelAndView();

		Url urlTemp = crawlerfile.getUrl();
		Url url = urlTemp == null ? null : CmiHibernateProxyUtil.deproxy(urlDAO.findUrlById(urlTemp.getId()));
		Filetype filetypeTemp = crawlerfile.getFiletype();
		Filetype filetype = filetypeTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(filetypeDAO.findFiletypeById(filetypeTemp.getId()));
		Crawlerfilestatus crawlerfilestatusTemp = crawlerfile.getCrawlerfilestatus();
		Crawlerfilestatus crawlerfilestatus = crawlerfilestatusTemp == null ? null
				: CmiHibernateProxyUtil
						.deproxy(crawlerfilestatusDAO.findCrawlerfilestatusById(crawlerfilestatusTemp.getId()));
		Crawlerfilestandard crawlerfilestandardTemp = crawlerfile.getCrawlerfilestandard();
		Crawlerfilestandard crawlerfilestandard = crawlerfilestandardTemp == null ? null
				: CmiHibernateProxyUtil
						.deproxy(crawlerfilestandardDAO.findCrawlerfilestandardById(crawlerfilestandardTemp.getId()));

		Crawlerfile existingCrawlerfile = CmiHibernateProxyUtil
				.deproxy(crawlerfileDAO.findCrawlerfileByPhysicalPrimaryKey(crawlerfile.getName()));

		crawlerfile.setUrl(url);
		crawlerfile.setFiletype(filetype);
		crawlerfile.setCrawlerfilestatus(crawlerfilestatus);
		crawlerfile.setCrawlerfilestandard(crawlerfilestandard);
		// 新建记录
		if (crawlerfile.getId() == null) {
			if (crawlerfile.equals(existingCrawlerfile)) {

				mav.addAllObjects(crawlerfileService.loadCrawlerfileClassReferencedSet(crawlerfile));
				// mav = editContinue(mav, crawlerfile);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerfile/editCrawlerfile");
			} else {
				crawlerfileService.saveCrawlerfile(crawlerfile);
				mav.setViewName("forward:/listCrawlerfiles");
			}
		}
		// 编辑记录
		else {
			Crawlerfile originCrawlerfile = CmiHibernateProxyUtil
					.deproxy(crawlerfileDAO.findCrawlerfileByPrimaryKey(crawlerfile.getId()));
			if (!crawlerfile.equals(originCrawlerfile) && crawlerfile.equals(existingCrawlerfile)) {
				mav.addAllObjects(crawlerfileService.loadCrawlerfileClassReferencedSet(crawlerfile));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerfile/editCrawlerfile");
			} else {
				crawlerfileService.saveCrawlerfile(crawlerfile);
				mav.setViewName("forward:/listCrawlerfiles");
			}
		}
		return mav;
	}

	@RequestMapping("/crawlerfileController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
